package no.ntnu.fp.gui.mainFrame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//import javax.swing.JFrame;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import no.ntnu.fp.client.Client;
import no.ntnu.fp.gui.EventDispatcher;
import no.ntnu.fp.gui.dialog.ParticipantDialog;
import no.ntnu.fp.gui.events.LogoutRequestedEvent;
import no.ntnu.fp.gui.events.RemoveEmployeeCalendarEvent;
import no.ntnu.fp.gui.events.RequestCalendarForEmployeeEvent;
import no.ntnu.fp.model.DialogModel;
import no.ntnu.fp.model.DialogModel.StateOfDialog;
import no.ntnu.fp.model.Employee;
import no.ntnu.fp.utils.ColorGenerator;

/**
 * This is the left-hand main toolbar in the main view.
 * @author Caroline
 *
 */

@SuppressWarnings("serial")
public class Toolbar extends JPanel {
	private JPanel calList;

	private static Toolbar instance;

	public static void createInstance(){
		if(instance == null){
			instance = new Toolbar();
		}else{
			throw new IllegalStateException("A toolbar instance already exists");
		}
	}
	public static Toolbar getInstance(){
		if(instance == null){
			throw new IllegalStateException("No instance of Toolbar created!");
		}else{
			return instance;
		}
	}

	private Toolbar() {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		JButton newEventBtn = new JButton("Ny hendelse");
		newEventBtn.addActionListener(new NewAppointmentButtonAction());
		add(newEventBtn);

		JButton addCalBtn = new JButton("<html>Legg til ny<br>kalender...</html>");
		addCalBtn.addActionListener(new ImportCalendarAction());
		add(addCalBtn);

		calList = new JPanel();
		calList.setLayout(new BoxLayout(calList, BoxLayout.Y_AXIS));
		JScrollPane scp = new JScrollPane(calList);
		add(scp);

		JButton logOutBtn = new JButton("Logg ut");
		logOutBtn.addActionListener(new LogoutButtonAction());
		add(Box.createVerticalGlue());
		add(logOutBtn);
	}

	private final class NewAppointmentButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			DialogModel.getInstance().setAppointment(StateOfDialog.APPOINTMENT, 
					null);
		}
	}

	private final class LogoutButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			String[] options = {"Logg ut", "Ikke logg ut"};
			int answer = JOptionPane.showOptionDialog(Toolbar.this.getParent(),
					"Ønsker du å logge ut?", "Logg ut?", JOptionPane.YES_NO_OPTION, 
					JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
			if(answer == JOptionPane.YES_OPTION){
				LogoutRequestedEvent evt = new LogoutRequestedEvent(this, Client.getEmployee());
				EventDispatcher.dispatchEvent(evt);
			}
		}
	}

	private void removeImportedCalendarPanel(Employee employee){
		for(int i = 0; i < calList.getComponentCount(); i++){
			if(((ImportedCalendarPanel)calList.getComponent(i)).getEmployee().equals(employee)){
				calList.remove(calList.getComponent(i));
			}
		}
	}

	private final class ImportedCalendarPanel extends JPanel{
		private final Employee employee;
		public ImportedCalendarPanel(Employee employee){
			this.employee = employee;
			this.setBackground(ColorGenerator.getColor(employee));
			JLabel name = new JLabel(employee.getName());
			EmployeeButton removeButton = new EmployeeButton("X", employee);
			removeButton.setForeground(Color.RED);
			removeButton.addActionListener(new ImportedCalendarButtonAction());
			add(name);
			add(removeButton);
			this.setPreferredSize(new Dimension(Toolbar.this.getWidth(), 20));
			this.setMaximumSize(new Dimension(Toolbar.this.getWidth(), 55));
		}
		public Employee getEmployee(){
			return this.employee;
		}
		private final class EmployeeButton extends JButton{
			private final Employee employee;
			public EmployeeButton(String text, Employee employee){
				super(text);
				this.employee = employee;
			}
			public Employee getEmployee(){
				return this.employee;
			}

		}
	}
	private final class ImportedCalendarButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Employee employee = ((no.ntnu.fp.gui.mainFrame.Toolbar.ImportedCalendarPanel.EmployeeButton)
					arg0.getSource()).getEmployee();
			String[] buttonNames = {"Fjern kalender", "Ikke fjern kalender"};
			int answer = JOptionPane.showOptionDialog(Toolbar.this.getParent(), 
					"Ønsker du å fjerne " + employee.getName() + " sine avtaler og møter fra din kalender?", 
					"Fjern kalender?", JOptionPane.YES_NO_OPTION, 
					JOptionPane.QUESTION_MESSAGE, null, buttonNames, buttonNames[0]);
			if(answer == JOptionPane.YES_OPTION){
				Toolbar.this.removeImportedCalendarPanel(employee);
				calList.repaint();
				EventDispatcher.dispatchEvent(new RemoveEmployeeCalendarEvent(this, employee));
			}
		}
	}
	private final class ImportCalendarAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			ParticipantDialog dia = ParticipantDialog.getInstance();
			dia.show(Toolbar.this.getParent());
			if(!dia.isCanceled()){
				for(Employee e : dia.getAllSelectedParticipants()){
					if(!contains(e)){
						calList.add(new ImportedCalendarPanel(e));
						EventDispatcher.dispatchEvent(new RequestCalendarForEmployeeEvent(this, e));
					}
				}
				calList.validate();
			}
		}
	}
	private boolean contains(Employee employee){
		for(int i = 0; i < calList.getComponentCount(); i++){
			if(((ImportedCalendarPanel)calList.getComponent(i)).getEmployee().equals(employee)){
				return true;
			}
		}
		return false;
	}
	public void addEmployeeCalendar(Employee employee){
		if(!contains(employee)){
			calList.add(new ImportedCalendarPanel(employee));
			EventDispatcher.dispatchEvent(new RequestCalendarForEmployeeEvent(this, employee));
		}
		calList.validate();
	}
}
